﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class PreallocatePickDetailDA : IPreallocatePickDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DO_CARTONIZE = "@DO_CARTONIZE";
        private string EFFECTIVE_DATE = "@EFFECTIVE_DATE";
        private string LOT = "@LOT";
        private string ORDER_ID = "@ORDER_ID";
        private string ORDER_LINE_ID = "@ORDER_LINE_ID";
        private string OWNER_ID = "@OWNER_ID";
        private string PACK_ID = "@PACK_ID";
        private string PICK_METHOD = "@PICK_METHOD";
        private string PREALLOCATE_PICK_CODE = "@PREALLOCATE_PICK_CODE";
        private string PREALLOCATE_PICK_DETAIL_ID = "@PREALLOCATE_PICK_DETAIL_ID";
        private string PREALLOCATE_RULE_ID = "@PREALLOCATE_RULE_ID";
        private string QTY = "@QTY";
        private string RUN_ID = "@RUN_ID";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_PREALLOCATE_PICK_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@PREALLOCATE_PICK_DETAIL WHERE PREALLOCATE_PICK_DETAIL_ID = @PREALLOCATE_PICK_DETAIL_ID ";
        private string SQL_DELETE_PREALLOCATE_PICK_DETAIL = " DELETE FROM @_@PREALLOCATE_PICK_DETAIL WHERE PREALLOCATE_PICK_DETAIL_ID = @PREALLOCATE_PICK_DETAIL_ID ";
        private string SQL_INSERT_PREALLOCATE_PICK_DETAIL = " INSERT INTO @_@PREALLOCATE_PICK_DETAIL ( WH_ID, PREALLOCATE_PICK_DETAIL_ID, ORDER_ID, ORDER_LINE_ID, OWNER_ID, SKU_ID, LOT, UOM_ID, UOM_QTY, QTY, PACK_ID, WAVE_ID, PREALLOCATE_RULE_ID, PREALLOCATE_PICK_CODE, DO_CARTONIZE, PICK_METHOD, RUN_ID, EFFECTIVE_DATE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @PREALLOCATE_PICK_DETAIL_ID, @ORDER_ID, @ORDER_LINE_ID, @OWNER_ID, @SKU_ID, @LOT, @UOM_ID, @UOM_QTY, @QTY, @PACK_ID, @WAVE_ID, @PREALLOCATE_RULE_ID, @PREALLOCATE_PICK_CODE, @DO_CARTONIZE, @PICK_METHOD, @RUN_ID, @EFFECTIVE_DATE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL = " SELECT PPD.ROWID, PPD.WH_ID, PPD.PREALLOCATE_PICK_DETAIL_ID, PPD.ORDER_ID,  PPD.ORDER_LINE_ID, PPD.OWNER_ID, PPD.SKU_ID, PPD.LOT, PPD.UOM_ID, PPD.UOM_QTY,  PPD.QTY, PPD.PACK_ID, PPD.WAVE_ID, PPD.PREALLOCATE_RULE_ID, PPD.PREALLOCATE_PICK_CODE,  PPD.DO_CARTONIZE, PPD.PICK_METHOD, PPD.RUN_ID, PPD.EFFECTIVE_DATE, PPD.TS, PPD.CREATED_BY,  PPD.CREATED_DATE, PPD.UPDATED_BY, PPD.UPDATED_DATE,ISNULL(SUM(PD.QTY),0) AS ALLOCATED_QTY   FROM  @_@PREALLOCATE_PICK_DETAIL PPD LEFT JOIN @_@PICK_DETAIL PD ON PD.PREALLOCATE_PICK_DETAIL_ID = PPD.PREALLOCATE_PICK_DETAIL_ID ";
        private string SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY = " GROUP BY PPD.ROWID, PPD.WH_ID, PPD.PREALLOCATE_PICK_DETAIL_ID, PPD.ORDER_ID,  PPD.ORDER_LINE_ID, PPD.OWNER_ID, PPD.SKU_ID, PPD.LOT, PPD.UOM_ID, PPD.UOM_QTY, PPD.QTY, PPD.PACK_ID, PPD.WAVE_ID, PPD.PREALLOCATE_RULE_ID, PPD.PREALLOCATE_PICK_CODE,  PPD.DO_CARTONIZE, PPD.PICK_METHOD, PPD.RUN_ID, PPD.EFFECTIVE_DATE, PPD.TS, PPD.CREATED_BY,  PPD.CREATED_DATE, PPD.UPDATED_BY, PPD.UPDATED_DATE  ";
        private string SQL_UPDATE_PREALLOCATE_PICK_DETAIL = " UPDATE @_@PREALLOCATE_PICK_DETAIL SET WH_ID = @WH_ID, ORDER_ID = @ORDER_ID, ORDER_LINE_ID = @ORDER_LINE_ID, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, LOT = @LOT, UOM_ID = @UOM_ID, UOM_QTY = @UOM_QTY, QTY = @QTY, PACK_ID = @PACK_ID, WAVE_ID = @WAVE_ID, PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID, PREALLOCATE_PICK_CODE = @PREALLOCATE_PICK_CODE, DO_CARTONIZE = @DO_CARTONIZE, PICK_METHOD = @PICK_METHOD, RUN_ID = @RUN_ID, EFFECTIVE_DATE = @EFFECTIVE_DATE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE PREALLOCATE_PICK_DETAIL_ID = @PREALLOCATE_PICK_DETAIL_ID  ";
        private string SQL_UPDATE_PREALLOCATE_PICK_DETAIL_QTY = " UPDATE @_@PREALLOCATE_PICK_DETAIL SET QTY = QTY + @QTY, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE PREALLOCATE_PICK_DETAIL_ID = @PREALLOCATE_PICK_DETAIL_ID ";
        private string TS = "@TS";
        private string UOM_ID = "@UOM_ID";
        private string UOM_QTY = "@UOM_QTY";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WAVE_ID = "@WAVE_ID";
        private string WH_ID = "@WH_ID";

        public bool CheckPreallocatePickDetailIDUnique(string preallocatePickDetailID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatePickDetailID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_PREALLOCATE_PICK_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckPreallocatePickDetailIDUnique(DataBase dataBase, DbTransaction tran, string preallocatePickDetailID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatePickDetailID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_PREALLOCATE_PICK_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeletePreallocatePickDetail(List<string> preallocatePickDetailID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < preallocatePickDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatePickDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeletePreallocatePickDetail(DataBase dataBase, DbTransaction tran, List<string> preallocatePickDetailID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < preallocatePickDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatePickDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllPreallocatePickDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllPreallocatePickDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public PreallocatePickDetailInfo GetPreallocatePickDetailByID(string preallocatepickdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.PREALLOCATE_PICK_DETAIL_ID = @PREALLOCATE_PICK_DETAIL_ID  " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatepickdetailID) };
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00003 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
                }
            }
            return preallocatePickDetailInfo;
        }

        public PreallocatePickDetailInfo GetPreallocatePickDetailByID(DataBase dataBase, DbTransaction tran, string preallocatepickdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.PREALLOCATE_PICK_DETAIL_ID = @PREALLOCATE_PICK_DETAIL_ID  " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatepickdetailID) };
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00003 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return preallocatePickDetailInfo;
        }

        public DataSet GetPreallocatePickDetailByQueryList(List<string> sqlWhere, PreallocatePickDetailQueryEntity preallocatepickdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!preallocatepickdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, preallocatepickdetailQuery.CurrentPage, preallocatepickdetailQuery.PageSize, preallocatepickdetailQuery.SortField, preallocatepickdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetPreallocatePickDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, PreallocatePickDetailQueryEntity preallocatepickdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!preallocatepickdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, preallocatepickdetailQuery.CurrentPage, preallocatepickdetailQuery.PageSize, preallocatepickdetailQuery.SortField, preallocatepickdetailQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<PreallocatePickDetailInfo> GetPreallocatePickDetailListByAllocateUomID(string orderID, int orderLineID, string uomID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.ORDER_ID = @ORDER_ID AND PPD.ORDER_LINE_ID = @ORDER_LINE_ID AND PPD.UOM_ID = @UOM_ID AND PPD.QTY > 0 " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_LINE_ID", orderLineID), new SqlParameter("@UOM_ID", uomID) };
            List<PreallocatePickDetailInfo> preallocatePickDetailInfos = new List<PreallocatePickDetailInfo>();
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00002 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
                    preallocatePickDetailInfos.Add(preallocatePickDetailInfo);
                }
            }
            return preallocatePickDetailInfos;
        }

        public List<PreallocatePickDetailInfo> GetPreallocatePickDetailListByAllocateUomID(DataBase dataBase, DbTransaction tran, string orderID, int orderLineID, string uomID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.ORDER_ID = @ORDER_ID AND PPD.ORDER_LINE_ID = @ORDER_LINE_ID AND PPD.UOM_ID = @UOM_ID AND PPD.QTY > 0 " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_LINE_ID", orderLineID), new SqlParameter("@UOM_ID", uomID) };
            List<PreallocatePickDetailInfo> preallocatePickDetailInfos = new List<PreallocatePickDetailInfo>();
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00002 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
                preallocatePickDetailInfos.Add(preallocatePickDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return preallocatePickDetailInfos;
        }

        public List<PreallocatePickDetailInfo> GetPreallocatePickDetailListByID(string orderID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.ORDER_ID = @ORDER_ID  " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID) };
            List<PreallocatePickDetailInfo> preallocatePickDetailInfos = new List<PreallocatePickDetailInfo>();
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00002 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
                    preallocatePickDetailInfos.Add(preallocatePickDetailInfo);
                }
            }
            return preallocatePickDetailInfos;
        }

        public List<PreallocatePickDetailInfo> GetPreallocatePickDetailListByID(DataBase dataBase, DbTransaction tran, string orderID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.ORDER_ID = @ORDER_ID  " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID) };
            List<PreallocatePickDetailInfo> preallocatePickDetailInfos = new List<PreallocatePickDetailInfo>();
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00002 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
                preallocatePickDetailInfos.Add(preallocatePickDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return preallocatePickDetailInfos;
        }

        public List<PreallocatePickDetailInfo> GetPreallocatePickDetailListByLineID(string orderID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.ORDER_ID = @ORDER_ID AND PPD.ORDER_LINE_ID = @ORDER_LINE_ID " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_LINE_ID", lineID) };
            List<PreallocatePickDetailInfo> preallocatePickDetailInfos = new List<PreallocatePickDetailInfo>();
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00002 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
                    preallocatePickDetailInfos.Add(preallocatePickDetailInfo);
                }
            }
            return preallocatePickDetailInfos;
        }

        public List<PreallocatePickDetailInfo> GetPreallocatePickDetailListByLineID(DataBase dataBase, DbTransaction tran, string orderID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID) + " WHERE PPD.ORDER_ID = @ORDER_ID AND PPD.ORDER_LINE_ID = @ORDER_LINE_ID " + this.SQL_SELECT_ALL_PREALLOCATE_PICK_DETAIL_GROUP_BY;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ORDER_ID", orderID), new SqlParameter("@ORDER_LINE_ID", lineID) };
            List<PreallocatePickDetailInfo> preallocatePickDetailInfos = new List<PreallocatePickDetailInfo>();
            PreallocatePickDetailInfo preallocatePickDetailInfo = null;
            using (IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    preallocatePickDetailInfo = new PreallocatePickDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_PICK_DETAIL_ID"].ToString(), reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["LOT"].ToString(), reader["UOM_ID"].ToString(), (reader["UOM_QTY"].ToString() != "") ? decimal.Parse(reader["UOM_QTY"].ToString()) : 0M, (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["PACK_ID"].ToString(), reader["WAVE_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["DO_CARTONIZE"].ToString(), reader["PICK_METHOD"].ToString(), reader["RUN_ID"].ToString(), (reader["EFFECTIVE_DATE"].ToString() != "") ? DateTime.Parse(reader["EFFECTIVE_DATE"].ToString()) : (CS00002 = new DateTime()), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), decimal.Parse(reader["ALLOCATED_QTY"].ToString()));
                    preallocatePickDetailInfos.Add(preallocatePickDetailInfo);
                }
            }
            return preallocatePickDetailInfos;
        }

        public int InsertPreallocatePickDetail(PreallocatePickDetailInfo preallocatePickDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                preallocatePickDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_PreallocatePickDetail_Parameters(preallocatePickDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertPreallocatePickDetail(DataBase dataBase, DbTransaction tran, PreallocatePickDetailInfo preallocatePickDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                preallocatePickDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_PreallocatePickDetail_Parameters(preallocatePickDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_PreallocatePickDetail_Parameters(PreallocatePickDetailInfo preallocatePickDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.PREALLOCATE_PICK_DETAIL_ID, SqlDbType.VarChar, 20), new SqlParameter(this.ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.ORDER_LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.LOT, SqlDbType.VarChar, 20), new SqlParameter(this.UOM_ID, SqlDbType.VarChar, 20), new SqlParameter(this.UOM_QTY, SqlDbType.Decimal, 13), new SqlParameter(this.QTY, SqlDbType.Decimal, 13), new SqlParameter(this.PACK_ID, SqlDbType.VarChar, 50), new SqlParameter(this.WAVE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.PREALLOCATE_RULE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.PREALLOCATE_PICK_CODE, SqlDbType.VarChar, 20), new SqlParameter(this.DO_CARTONIZE, SqlDbType.VarChar, 1), new SqlParameter(this.PICK_METHOD, SqlDbType.VarChar, 1), 
                new SqlParameter(this.RUN_ID, SqlDbType.VarChar, 20), new SqlParameter(this.EFFECTIVE_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20)
             };
            if (!string.IsNullOrEmpty(preallocatePickDetailInfo.WhID))
            {
                paramArray[0].Value = preallocatePickDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = preallocatePickDetailInfo.PreallocatePickDetailID;
            paramArray[2].Value = preallocatePickDetailInfo.OrderID;
            paramArray[3].Value = preallocatePickDetailInfo.OrderLineID;
            paramArray[4].Value = preallocatePickDetailInfo.OwnerID;
            paramArray[5].Value = preallocatePickDetailInfo.SkuID;
            paramArray[6].Value = preallocatePickDetailInfo.Lot;
            paramArray[7].Value = preallocatePickDetailInfo.UomID;
            paramArray[8].Value = preallocatePickDetailInfo.UomQty;
            paramArray[9].Value = preallocatePickDetailInfo.Qty;
            paramArray[10].Value = preallocatePickDetailInfo.PackID;
            paramArray[11].Value = preallocatePickDetailInfo.WaveID;
            paramArray[12].Value = preallocatePickDetailInfo.PreallocateRuleID;
            paramArray[13].Value = preallocatePickDetailInfo.PreallocatePickCode;
            paramArray[14].Value = preallocatePickDetailInfo.DoCartonize;
            paramArray[15].Value = preallocatePickDetailInfo.PickMethod;
            paramArray[0x10].Value = preallocatePickDetailInfo.RunID;
            paramArray[0x11].Value = preallocatePickDetailInfo.EffectiveDate;
            paramArray[0x12].Value = preallocatePickDetailInfo.CreatedBy;
            paramArray[0x13].Value = preallocatePickDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdatePreallocatePickDetail(PreallocatePickDetailInfo preallocatePickDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_PreallocatePickDetail_Parameters(preallocatePickDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdatePreallocatePickDetail(DataBase dataBase, DbTransaction tran, PreallocatePickDetailInfo preallocatePickDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_PreallocatePickDetail_Parameters(preallocatePickDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PREALLOCATE_PICK_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdatePreAllocatePickDetailQty(string preallocatePickDetailID, decimal qty, string updateBy, string whLoginID)
        {
            string sql = this.SQL_UPDATE_PREALLOCATE_PICK_DETAIL_QTY.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@QTY", qty), new SqlParameter("@UPDATED_BY", updateBy), new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatePickDetailID) };
            return DBHelper.ExecuteNonQuery(CommandType.Text, sql, paras);
        }

        public int UpdatePreAllocatePickDetailQty(DataBase dataBase, DbTransaction tran, string preallocatePickDetailID, decimal qty, string updateBy, string whLoginID)
        {
            string sql = this.SQL_UPDATE_PREALLOCATE_PICK_DETAIL_QTY.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@QTY", qty), new SqlParameter("@UPDATED_BY", updateBy), new SqlParameter("@PREALLOCATE_PICK_DETAIL_ID", preallocatePickDetailID) };
            return dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
        }
    }
}

